<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/22
 * Time: 15:49
 */

namespace app\bbs\api;

use app\bbs\model\ForumAdmingroup;
use app\bbs\model\ForumUserAdmingroup;
use app\bbs\api\ForumPostApi;
use app\bbs\api\ForumForumApi;
use app\common\api\BaseApi;
use app\system\api\MemberApi;

class ForumUserAdmingroupApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "bbs/ForumUserAdmingroupApi/add";
    /**
     * 保存
     */
    const SAVE = "bbs/ForumUserAdmingroupApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "bbs/ForumUserAdmingroupApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "bbs/ForumUserAdmingroupApi/delete";

    /**
     * 查询
     */
    const QUERY = "bbs/ForumUserAdmingroupApi/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "bbs/ForumUserAdmingroupApi/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "bbs/ForumUserAdmingroupApi/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "bbs/ForumUserAdmingroupApi/queryNoPaging";

    /**
     * 关小黑屋
     */
    const BAN_USER = "bbs/ForumUserAdmingroupApi/banUser";


    /**
     * 删回帖
     */
    const DELETE_POST = "bbs/ForumUserAdmingroupApi/deletePost";


    /**
     * 权限查询
     */
    const POWER    = "bbs/ForumUserAdmingroupApi/power";
    const IS_ADMIN = "bbs/ForumUserAdmingroupApi/isAdmin";

    protected function _init(){
        $this -> model = new ForumUserAdmingroup();
    }

    /*
     * say      : is admin by uid and fid
     * return   : api reutn
     */
    public function isAdmin($uid=0,$fid=0){
        $uid = intval($uid);
        $fid = intval($fid);
        $result = apiCall(ForumUserAdmingroupApi::GET_INFO,[['uid'=>$uid]]);
        if(false === $result['status']) return $this->apiReturnErr('获取用户权限失败');
        //非admin
        if(empty($result['info'])) return $this->apiReturnSuc(false);
        $admin_fid = intval($result['info']['fid']);
        //
        if($fid === $admin_fid)  return $this->apiReturnSuc(true);
        if(0 === $admin_fid)  return $this->apiReturnSuc(true);
        //this user admin fids
        $r = apiCall(ForumForumApi::GET_FID_WITH_CHILD,[$admin_fid]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(in_array($fid,$r['info']))  return $this->apiReturnSuc(true);
        return $this->apiReturnSuc(false);
    }
    /*
     * say      : check if user has forum power
     * return   : api return
     */
    public function hasPower($uid=0,$fid=0,$power=""){
        $uid = intval($uid);
        $fid = intval($fid);
        if(!defined(ForumAdmingroup::$power)) return $this->apiReturnErr('power error');
        $power = ForumAdmingroup::$power;
        $r = $this->isAdmin($uid,$fid);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(!$r['info'])   return $this->apiReturnSuc(false);
        //is admin now
        $r = $this->getPower($uid);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(!$r['info'])   return $this->apiReturnSuc(false);
        if(isset($r['info'][$power]) && 1===intval($r['info'][$power]))
            return $this->apiReturnSuc(true);
        else
            return $this->apiReturnSuc(false);
    }

    /**
     * 删回帖
     */
    public function deletePost($admin_uid,$fid,$tid,$pid){
        $result = $this->power($admin_uid);
        if($result['status']){
            $power = $result['info'];
            if($power[ForumAdmingroup::ALLOW_DEL_POST]!=1){
                return $this->apiReturnErr('该用户没有删帖权限');
            }elseif($power['fid']!=0 && $power['fid']!=$fid){
                return $this->apiReturnErr('该用户没有在该论坛的删帖权限');
            }

            $map = [
                'fid' => $fid,
                'tid' => $tid,
                'pid' => $pid
            ];
            $result = apiCall(ForumPostApi::SAVE,[$map,['status'=>-1]]);
            if($result['status']){
                return $this->apiReturnSuc('删除成功');
            }else{
                return $this->apiReturnErr('删除失败');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }

    }


    /**
     * 禁言
     */
    public function banUser($admin_uid,$uid,$banned){

        //用户检测
        $result = apiCall(MemberApi::GET_INFO,[['uid'=>$uid]]);
        if(!$result['status']) return $this->apiReturnErr('服务器故障：member get');
        if(empty($result['info'])) return $this->apiReturnErr('无此用户');
        //用户是否为管理
        $result = apiCall(ForumUserAdmingroupApi::GET_INFO,[['uid'=>$uid]]);
         if(!$result['status']) return $this->apiReturnErr('服务器故障：admin get');
        if(!empty($result['info'])) return $this->apiReturnErr('不允许禁言管理');
        if($admin_uid==$uid){
            return $this->apiReturnErr('admin_uid不能与uid相同');
        }
        //管理权限检测
        $result  = $this->power($admin_uid);
        // trace('==banuser==');
        // trace($result);
        if($result['status']){
            $power = $result['info'];
            if(!is_array($power)) return $this->apiReturnErr($power);
            if(1 !== intval($power[ForumAdmingroup::ALLOW_BAN_USER])){
                return $this->apiReturnErr('该管理人员没有禁言权限');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }

        $banned = intval($banned);
        $map    = [
        'uid'        =>$uid,
        'start_time' =>NOW_TIME,
        'end_time'   =>NOW_TIME+$banned*60,
        ];
        $where  = ' `uid`=\''.$uid.'\' and `end_time`>'.NOW_TIME.' ';
        $result = apiCall(ForumSilentApi::GET_INFO,[$where,'end_time desc']);
        if(false === $result['status']) return $this->apiReturnErr('服务器故障：silent get');
        if(!empty($result['info'])){
            //禁言中，覆盖
            $result = apiCall(ForumSilentApi::SAVE,[['id'=>$result['info']['id']],$map]);
            $str = '重新';
        }else{
            $result = apiCall(ForumSilentApi::ADD,[$map]);
            $str = '';
        }
        if(!$result['status']) return $this->apiReturnErr('服务器故障：silent add');
        return $this->apiReturnSuc($str.'禁言成功');
    }


    public function power($uid){
        $result = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
        if($result['status']){
            if(empty($result['info'])){
                return $this->apiReturnErr('该管理用户不存在');
            }
        }else{
            return $this->apiReturnErr($result['info']);
        }

        $result = apiCall(ForumUserAdmingroupApi::GET_INFO,array(array('uid'=>$uid)));

        if($result['status']){

            if(empty($result['info'])){
                return $this->apiReturnSuc('该用户没有特殊权限');
            }else{
                $adminid = $result['info']['adminid'];
                $fid = $result['info']['fid'];
            }

        }else{
            return $this->apiReturnErr('获取用户权限失败');
        }

        $result = apiCall(ForumAdmingroupApi::GET_INFO,array(array('adminid'=>$adminid)));

        if($result['status']){

            if(empty($result['info'])){
                return $this->apiReturnSuc('该用户没有特殊权限');
            }else{
                $result['info']['fid'] = $fid;
                return $this->apiReturnSuc($result['info']);
            }

        }else{
            return $this->apiReturnErr('获取用户权限失败');
        }

    }
    /******** private function *********************/
    /*
     * say      : 
     * return   : api return
     */
    private function getPower($uid){
        $r = $this->model->alias('g')
        ->join('left join __FORUM_ADMINGROUP__ p on p.adminid = g.adminid')
        ->where('g.`uid`='.$uid)
        ->field('p.*,g.fid');
        if(false === $r) return $this->apiReturnErr($this->model->getDbError());
        return $this->apiReturnSuc($r);
    }
}